package com.situ.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.blog.mapper.BlogMapper;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.vo.BlogCountVO;
import com.situ.blog.service.IBlogService;
import com.situ.blog.util.JSONResult;
import com.situ.blog.util.LayUITableJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// JavaEE
// 浏览器->Controller->Service->Mapper(Dao)
@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Blog> selectAll() {
        return blogMapper.selectAll();
    }

    @Override
    public LayUITableJSONResult selectByPage(Integer page, Integer limit) {
        //开启分页，page：第几页，limit：每页有多少条
        PageHelper.startPage(page, limit);
        // 会在查找所有后面拼接分页信息，并不是查找所有，limit
        List<Blog> list = blogMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        //返回数据库中总的数量，返回给前台算一共有多少页
        long totalCount = pageInfo.getTotal();

        return LayUITableJSONResult.ok((int)totalCount, list);
    }

    @Override
    public JSONResult deleteById(Integer id) {
        int count = blogMapper.deleteById(id);
        return count == 1 ? JSONResult.ok() : JSONResult.error();
    }

    @Override
    public JSONResult add(Blog blog) {
        int count = blogMapper.add(blog);
        return count == 1 ? JSONResult.ok() : JSONResult.error();
    }

    @Override
    public JSONResult selectBlogCount() {
        List<BlogCountVO> list = blogMapper.selectBlogCount();
        return JSONResult.ok("成功", list);
    }
}
